import { MES_SMH_ApiHost } from '@/utils/Interceptors'

// 报工数据类型 - 根据Swagger文档更新
export interface WorkReport {
  workReportId: number
  workOrderNo: string
  taskName: string
  product: string
  productCode: string
  productName: string
  productSpec: string
  taskStatus: string
  productionPersonnel: string
  reportedQuantity: number
  unit: string
  goodQuantity: number
  defectiveQuantity: number
  processProgress: string
  startTime: string
  endTime: string
  reportDurationHours: number
  reportDurationMinutes: number
  standardEfficiency: number
  actualEfficiency: number
  complianceRate: number
  pricingMethod?: string
  unitPrice?: number
  estimatedWage?: number
  approvalStatus?: string
  approver?: string
  reportTime?: string
  createTime?: string
  creator?: string
  modifier?: string
  modifyTime?: string
}

// API响应类型
export interface ApiResponse<T> {
  code: number
  msg: string
  data: T
}

// 分页响应类型 - 根据Swagger文档更新
export interface PagedResponse<T> {
  totalCount: number
  items: T[]
}

// 高级查询参数类型 - 根据Swagger文档更新
export interface AdvancedQueryParams {
  taskName?: string
  productCode?: string
  productName?: string
  taskStatus?: string
  productionPersonnel?: string
  reportedQuantity?: number
  workOrderNo?: string
  unit?: string
  goodQuantity?: number
  startTimeFrom?: string
  startTimeTo?: string
  endTimeFrom?: string
  endTimeTo?: string
  pageIndex: number
  pageSize: number
}

// 创建报工类型
export interface CreateWorkReport {
  workOrderNo: string
  taskName: string
  product?: string
  productCode: string
  productName: string
  productSpec?: string
  taskStatus: string
  productionPersonnel: string
  reportedQuantity: number
  unit: string
  goodQuantity: number
  defectiveQuantity: number
  processProgress?: number
  startTime: string
  endTime: string
  standardEfficiency?: number
  pricingMethod?: string
  wageUnitPrice?: number
  awardAmount?: number
  penaltyAmount?: number
  estimatedWage?: number
  reportDurationHours?: number
  reportDurationMinutes?: number
  actualEfficiency?: number
  complianceRate?: number
}

// 更新报工类型
export interface UpdateWorkReport {
  workReportId: number
  taskName?: string
  taskStatus?: string
  productionPersonnel?: string
  reportedQuantity?: number
  goodQuantity?: number
  defectiveQuantity?: number
  processProgress?: string
  startTime?: string
  endTime?: string
  actualEfficiency?: number
}

// 获取报工分页列表（高级查询）
export const getWorkReportsAdvanced = async (params: AdvancedQueryParams) => {
  return await MES_SMH_ApiHost.post<ApiResponse<PagedResponse<WorkReport>>>('/api/WorkReport/GetAdvancedPageList', params)
}

// 创建报工
export const createWorkReport = async (data: CreateWorkReport) => {
  // 后端控制器 Create([FromBody] WorkReportCreateDTO createDto) 直接接收 DTO，本函数直接发送 DTO 本体
  return await MES_SMH_ApiHost.post<ApiResponse<WorkReport>>('/api/WorkReport/Create', data)
}

// 更新报工
export const updateWorkReport = async (data: UpdateWorkReport) => {
  return await MES_SMH_ApiHost.put<ApiResponse<WorkReport>>(`/api/WorkReport/Update/${data.workReportId}`, data)
}

// 删除报工
export const deleteWorkReport = async (workReportId: number) => {
  return await MES_SMH_ApiHost.delete<ApiResponse<boolean>>(`/api/WorkReport/Delete/${workReportId}`)
}

// 批量删除报工
export const batchDeleteWorkReports = async (workReportIds: number[]) => {
  return await MES_SMH_ApiHost.post<ApiResponse<boolean>>('/api/WorkReport/BatchDelete', { workReportIds })
}

// 导出报工数据
export const exportWorkReports = async (params: AdvancedQueryParams) => {
  return await MES_SMH_ApiHost.post('/api/WorkReport/Export', params, {
    responseType: 'blob'
  })
}


